package network2;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.xml.crypto.dsig.keyinfo.KeyValue;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import javax.swing.JTextPane;

public class URLConnectionEx extends JFrame {

	private JPanel contentPane;
	private JPanel panel;
	private JButton btnNewButton;
	private JTextField domain;
	private JScrollPane scrollPane;
	private JTextPane result;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					URLConnectionEx frame = new URLConnectionEx();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public void process(){
		String type = "utf-8";
		String d = domain.getText();
		if(!d.contains("http://")) d = "http://" + d;
		result.setText("");
		StringBuilder sb = new StringBuilder();
		
		try{
			URL url = new URL(d);
			
			sb.append("<img src='file:\\p:\\mypic.jpg'></img><p/>");
			
			sb.append("<b><font color='red'>호스트명 : </font></b>" + url.getHost() + "<br/>");
			sb.append("<b><font color='red'>포트 : </font></b>" + url.getPort());
			sb.append("<hr/>");

			URLConnection uc = url.openConnection();
			sb.append("<b><font color='red'>mime type : </font></b>" + uc.getContentType() + "<br/>");
			sb.append("<b><font color='red'>Encoding : </font></b>" + uc.getContentEncoding() + "<br/>");
			sb.append("<b><font color='red'>Header : </font></b>" + uc.getHeaderFields());
			sb.append("<hr/>");

			String temp = uc.getContentType().toLowerCase();
			if(temp.contains("euc-kr")){
				type = "euc-kr";
			}
			
			InputStream is = uc.getInputStream();
			InputStreamReader isr = new InputStreamReader(is,type);
			BufferedReader br = new BufferedReader(isr);
			
			String str = "";
			sb.append("<xmp");
			while(true){
				str = br.readLine();
				if(str == null) break;
				
				sb.append(str + "\n");
			}
			sb.append("</xmp>");
			result.setText(sb.toString());
			
			br.close();
			isr.close();
			is.close();
			
		}catch(Exception ex){
			sb.append("<p>" + ex.toString() + "</p>");
		}
		
	}
	
	public URLConnectionEx() {
		setTitle("URLConnection Example");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 567, 513);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		contentPane.add(getPanel(), BorderLayout.NORTH);
		contentPane.add(getScrollPane(), BorderLayout.CENTER);
	}

	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setLayout(new BorderLayout(0, 0));
			panel.add(getBtnNewButton(), BorderLayout.EAST);
			panel.add(getDomain(), BorderLayout.CENTER);
		}
		return panel;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("확인");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					process();
				}
			});
		}
		return btnNewButton;
	}
	private JTextField getDomain() {
		if (domain == null) {
			domain = new JTextField();
			domain.setText("www.naver.com");
			domain.addKeyListener(new KeyAdapter() {
				@Override
				public void keyReleased(KeyEvent e) {
					if(e.getKeyCode() == KeyEvent.VK_ENTER){
						process();
					}
				}
			});
			domain.setColumns(10);
		}
		return domain;
	}
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setViewportView(getResult());
		}
		return scrollPane;
	}
	private JTextPane getResult() {
		if (result == null) {
			result = new JTextPane();
			result.setContentType("text/html");
		}
		return result;
	}
}
